package com.example.agriculture.service;

import com.example.agriculture.dao.*;
import com.example.agriculture.entity.CommonUse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.sql.ResultSet;
import java.util.List;
import java.util.UUID;

@Service
public class CommonUseService {
    private String http = "http://localhost:8081/sdk";
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private PlanterDao planterDao;
    @Autowired
    private ProcessDao processDao;
    @Autowired
    private LogisticsDao logisticsDao;
    @Autowired
    private SaleDao saleDao;
    @Autowired
    private CommonUseDao commonUseDao;


    //统一查看   访问该方法需要两个条件 userId 和 userFlag
    public List<CommonUse> view(CommonUse commonUse){
        String userFlag = commonUse.getUserFlag();
        if("1".equals(userFlag)){
           return  planterDao.view(commonUse);
        }else if("2".equals(userFlag)){
            return processDao.view(commonUse);
        }else if("3".equals(userFlag)){
            return logisticsDao.view(commonUse);
        }else if ("4".equals(userFlag)){
            return saleDao.view(commonUse);
        }
        return null;
    }





//统一编辑
    public int edit(CommonUse commonUse){
        int result = -1;
        String userFlag = commonUse.getUserFlag();
        if("1".equals(userFlag)){
            //没有ID,表示没有数据，执行插入操作

            int res = commonUse.getPlanterId();
            if(0==res || "0".equals(String.valueOf(res))) {
                result= add(commonUse);
                sdk(commonUse,result);
                return result;
            }
            result = planterDao.edit(commonUse);
            sdk(commonUse,result);
            return result;
        }else if("2".equals(userFlag)){
            //没有ID,表示没有数据，执行插入操作
            int res = commonUse.getProcessId();
            if(0==res || "0".equals(String.valueOf(res))) {
                result = add(commonUse);
                sdk(commonUse,result);
                return result;
            }
            result = processDao.edit(commonUse);
            sdk(commonUse,result);
            return result;
        }else if("3".equals(userFlag)){
            //没有ID,表示没有数据，执行插入操作
            int res = commonUse.getLogisticsId();
            if(0==res || "0".equals(String.valueOf(res))){
                result= add(commonUse);
                sdk(commonUse,result);
                return result;
            }
            result = logisticsDao.edit(commonUse);
            sdk(commonUse,result);
            return result;
        }else if ("4".equals(userFlag)){
            //没有ID,表示没有数据，执行插入操作
            int res = commonUse.getSaleId();
            if(0==res || "0".equals(String.valueOf(res))){
                result = add(commonUse);
                sdk(commonUse,result);
                return result;
            }
            result= saleDao.edit(commonUse);
            sdk(commonUse,result);
            return result;
        }
        return -1;
    }

//统一插入

    public int add(CommonUse commonUse){
        String userFlag = commonUse.getUserFlag();
        if("1".equals(userFlag)){
            commonUse.setPlanterOnlyKey(UUID.randomUUID().toString());
            return   planterDao.insert(commonUse);
        }else if("2".equals(userFlag)){
            return   processDao.insert(commonUse);
        }else if("3".equals(userFlag)){
            return logisticsDao.insert(commonUse);
        }else if ("4".equals(userFlag)){
            return saleDao.insert(commonUse);
        }
        return -1;
    }

//sdk通信
    public void sdk(CommonUse commonUse,int res){
        if(res>0) {
            CommonUse view = commonUseDao.view(commonUse);
            String s = "\"userid\":\"" + commonUse.getUserId()
                    + "\",\"temperature\":\"" + commonUse.getTemperature()
                    + "\",\"envhumidity\":\"" + commonUse.getEnvHumidity()
                    + "\",\"ph\":\"" + commonUse.getPh()
                    + "\",\"light\":\"" + commonUse.getLight()
                    + "\",\"soilhumidity\":\"" + commonUse.getSoilHumidity()
                    + "\",\"factoryname\":\"" + commonUse.getFactoryName()
                    + "\",\"qualityinspector\":\"" + commonUse.getQualityInspector()
                    + "\",\"processperson\":\"" + commonUse.getProcessPerson()
                    + "\",\"date\":\"" + commonUse.getDate()
                    + "\",\"location\":\"" + commonUse.getLocation()
                    + "\",\"intime\":\"" + commonUse.getInTime()
                    + "\",\"outtime\":\"" + commonUse.getOutTime()
                    + "\",\"transportperson\":\"" + commonUse.getTransportPerson()
                    + "\",\"leaveaddress\":\"" + commonUse.getLeaveAddress()
                    + "\",\"arriveaddress\":\"" + commonUse.getArriveAddress()
                    + "\",\"mallname\":\"" + commonUse.getMallName()
                    + "\",\"saletime\":\"" + commonUse.getSaleTime()
                    + "\",\"price\":\"" + commonUse.getPrice()
                    + "\",\"role\":\"" + commonUse.getUserName() + "\"";
            String URL_invoke = http + "/invoke?id=" + commonUse.getPlanterOnlyKey() + "&data=" + s;
            ResponseEntity<String> forEntity = restTemplate.getForEntity(URL_invoke, String.class);
        }
    }


}
